package com.qf.fronted.service.impl;

import com.qf.fronted.domain.CartMap;
import com.qf.fronted.mapper.CartMapper;
import com.qf.fronted.service.ICartService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;

@Service
public class CartServiceImpl implements ICartService {

    private CartMapper cartMapper;

    public CartServiceImpl(CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }

    /**
     * 合并浏览器端与服务端的购物车数据
     *
     * @return
     * @Param browserCartData 是浏览器购物车数据
     */
    @Override
    public Map<String, Integer> mergeCartData(Map<String, Integer> browserCartData, Integer userId) {
        Map<String, Integer> serverCartData = new HashMap<>(8);
        Set<String> goodIdSet = new HashSet<>();
        // 用户存在于数据库的购物车数据
        List<CartMap> cartData = cartMapper.getCartInfoOfUser(userId);
        for (CartMap cart : cartData) {
            serverCartData.put(cart.getGoodId(), cart.getNum());
        }
        cartData.forEach((cart) -> {
            if (browserCartData.containsKey(cart.getGoodId())) {
                goodIdSet.add(cart.getGoodId());
            }
        });
        try {
            Set<String> keySet = browserCartData.keySet();
            for (String goodId : keySet) {
                // 获取对应的商品的数量
                Integer num = browserCartData.get(goodId);
                // 表示交集部分，需要更新数据库的数据
                if (goodIdSet.contains(goodId)) {
                    cartMapper.updateShoppingCartData(Integer.parseInt(goodId), num, userId, new Date());
                } else { //浏览器中有，但是数据库中没有需要更新
                    cartMapper.insertShoppingCartData(Integer.parseInt(goodId), num, userId, new Date());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    @Override
    public Map<String, Integer> getCartInfoOfUser(Integer userId) {
        List<CartMap> cartData = cartMapper.getCartInfoOfUser(userId);
        Map<String, Integer> serverCartData = new HashMap<>(8);
        for (CartMap cart : cartData) {
            serverCartData.put(cart.getGoodId(), cart.getNum());
        }
        return serverCartData;

    }

    @Override
    public void deleteCartData(Integer userId, Integer goodId) {
        cartMapper.deleteCartData(userId, goodId);
    }

    @Override
    public void addGood(Integer userId, Integer goodId) {
        // 获取对应用户的对应商品的数量，如果没有返回 null
        Integer num = cartMapper.getNumOfSpecifyGood(goodId, userId);
        // 如果数量为null, 表示没有对应的商品
        try {
            if (null == num) {
                cartMapper.insertShoppingCartData(goodId, 1, userId, new Date());
            } else { //如果数量不为null, 表示有对应的商品
                cartMapper.updateShoppingCartData(goodId, num + 1, userId, new Date());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void updateGoodNum(Integer userId, Integer goodId, Integer num) {
        try {
            cartMapper.updateShoppingCartData(goodId, num, userId, new Date());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
